package fr.afcepf.atod.gestionVP.bean;

import java.util.ArrayList;
import java.util.Collection;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;

import fr.afcepf.atod.gestionVP.data.Client;
import fr.afcepf.atod.gestionVP.data.Commercial;
import fr.afcepf.atod.gestionVP.facades.FacadeGVP;
import fr.afcepf.atod.gestionVP.facades.IFacadeGVP;



@ManagedBean
@SessionScoped
public class GestionVenteBean {

	private String montant;
	private String date;
	private String duree;
	private Commercial selectedCom;
	private Client selectedClient;
	private IFacadeGVP facade = FacadeGVP.getInstance();
	
	public GestionVenteBean() {	
		try {
			ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
			facade = FacadeGVP.getInstance(servletContext);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getMontant() {
		return montant;
	}
	
	public void setMontant(String montant) {
		this.montant = montant;
	}
	
	public String getDate() {
		return date;
	}
	
	public void setDate(String date) {
		this.date = date;
	}
	
	public String getDuree() {
		return duree;
	}
	
	public void setDuree(String duree) {
		this.duree = duree;
	}

	public Commercial getSelectedCom() {
		return selectedCom;
	}
	
	public void setSelectedCom(Commercial selectedCom) {
		this.selectedCom = selectedCom;
	}

	public Client getSelectedClient() {
		return selectedClient;
	}
	
	public void setSelectedClient(Client selectedClient) {
		this.selectedClient = selectedClient;
	}
	
	public Collection<SelectItem> getComList(){
		Collection<SelectItem> listeSel = new ArrayList<SelectItem>();
		for (Commercial c : this.facade.getServiceVente().getAllCom()) {
			listeSel.add(new SelectItem(c.getId(),c.getId()+" - "+c.getNom()+" "+c.getPrenom()+" ("+c.getPole()+")"));
		}
		return listeSel;
	}
	
	public Collection<SelectItem> getClientsPossible(){
		Collection<SelectItem> listeSel = new ArrayList<SelectItem>();
		if (selectedCom != null) {
			for (Client c : this.facade.getServiceVente().getClientByCom(this.selectedCom.getId())) {
				listeSel.add(new SelectItem(c.getId(),c.getId()+" - "+c.getNom()+" ("+c.getTypeSociete()+")"));
			}
		}
		return listeSel;
	}
}
